package fr.oz.mobile.module.profile.nav 
{
	import caurina.transitions.Tweener;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import fr.oz.core.events.ProfilEvent;
	import fr.oz.mobile.common.ui.DragDropableList;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class ProfilNavItemActivator extends ProfilNavItemActivator_FC
	{
		
		private var _activate:Boolean;
		
		public function ProfilNavItemActivator(active:Boolean) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			_activate = active;
			
			if (active) {
				bg_mask.x = puce.x = 70;
			}
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			puce.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
		}
		
		private function handleMouseDown(e:MouseEvent):void 
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			dispatchEvent(new ProfilEvent(ProfilEvent.PROFIL_HANDLE, null, true));
			DragDropableList.DRAGABLE = false;
			addEventListener(Event.ENTER_FRAME, onFrame);
			puce.startDrag(false, new Rectangle(19, 20, 51, 0));
		}
		
		private function onFrame(e:Event):void 
		{
			bg_mask.x = puce.x;
		}
		
		private function handleMouseUp(e:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			removeEventListener(Event.ENTER_FRAME, onFrame);
			puce.stopDrag();
			DragDropableList.DRAGABLE = true;
			dispatchEvent(new ProfilEvent(ProfilEvent.PROFIL_FREE, null, true));
			if (puce.x < 45) {
				Tweener.addTween([puce, bg_mask], { x:19, time:(puce.x - 19)/71 } );
				_activate = false;
				dispatchEvent(new Event(Event.CHANGE));
			}
			
			if (puce.x >= 45) {
				Tweener.addTween([puce, bg_mask], { x:70, time:(70 - puce.x)/70 } );
				_activate = true;
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		
		public function isActivate():Boolean { return _activate; }
		
		
	}

}